package com.sc.nft.admin.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.*;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.BoxTakePoolDao;
import com.sc.nft.dao.DigitalCollectionProductInfoDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.BoxTakePool;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.vo.BoxPoolVO;
import com.sc.nft.entity.vo.BoxTakeConversionConditionCombination;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.enums.CommodityType;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.BoxTakeConversionConditionService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/voucherConversion")
@Api(tags = "商品兑换NFT的后台")
public class VoucherConversionNFTController {


    private final BoxTakePoolDao boxTakePoolDao;

    private final EquityPropsDao equityPropsDao;

    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;

    private final BoxTakeConversionConditionService boxTakeConversionConditionService;

    private final UserCreatorInfoDao userCreatorInfoDao;


    @PostMapping(value = "/addVoucherConversion", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "添加商品兑换", notes = "添加商品兑换", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addBoxPool(@Valid @RequestBody AddBoxPoolRequest request) {
        request.getEcologyCastGas().checkEcologyCastGas();
        DigitalCollectionProductInfo blindBoxDetails = digitalCollectionProductInfoDao.getAllBlindProductDetails(request.getProductBoxId(), 0);
        if (ObjectUtil.isNull(blindBoxDetails)) {
            return Result.fail("商品不存在");
        }
        Assert.isTrue(blindBoxDetails.getCommodityType() == CommodityType.EXCHANGE_COLLECTION,
                () -> new GlobalRunTimeException("只有兑换商品才能添加"));
        boxTakeConversionConditionService.addBoxCondition(true, null, 2, blindBoxDetails.getId(), request.getUpperLimit(), request.getAddBoxConditionCollectionDTO(), request.getAddBoxCondition(), request.getConvertScenario(), request.getDaoId(),request.getEcologyCastGas());
        return Result.success();
    }

//    @PostMapping(value = "/editVoucherConversion", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "编辑商品兑换", notes = "编辑商品兑换", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
//    public Result editBoxPool(@Valid @RequestBody EditBoxPoolRequest request) {
//        BoxTakePool boxTakePool = boxTakePoolDao.getById(request.getId());
//        if (ObjectUtil.isNull(boxTakePool)) {
//            return Result.fail("未找到兑换商品");
//        }
//        EquityProps equityProps = equityPropsDao.getById(request.getPropsId());
//
//        if (ObjectUtil.isNull(equityProps)) {
//            return Result.fail("未找到虚拟道具");
//        }
//        if (equityProps.getTypeClassify() != EquityPropsTypeEnum.VIRTUAL_PROPS) {
//            return Result.fail("虚拟道具类型不正确");
//        }
//        boxTakePool.setPropsId(equityProps.getId());
//        boxTakePool.setPropsName(equityProps.getFirstTitle());
//        boxTakePool.setCostQuantity(request.getCostQuantity());
//        boxTakePool.updateById();
//        return Result.success();
//    }

//    @PostMapping(value = "/removeVoucherConversion", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiOperation(value = "删除藏品兑换", notes = "删除藏品兑换", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @SysMemberLogin
//    public Result removeBoxPool(@Valid @RequestBody IdRequest request) {
//        BoxTakePool boxTakePool = boxTakePoolDao.getById(request.getId());
//        if (ObjectUtil.isNull(boxTakePool)) {
//            return Result.fail("未找到兑换商品");
//        }
//        boxTakePool.deleteById();
//        return Result.success();
//    }

    @PostMapping(value = "/voucherConversionPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品兑换列表", notes = "商品兑换列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<BoxPoolVO> voucherConversion(@RequestBody @Valid BoxPoolSearchRequest request) {
        Page<BoxPoolVO> boxPoolVOPage = boxTakePoolDao.voucherConversion(null, request.getBoxName(), request.getConvertScenario(), request.getDaoId(), request.getPageNo(), request.getPageSize());
        boxPoolVOPage.getRecords().forEach(boxPoolVO -> {
            BoxTakeConversionConditionCombination boxTakeConversionConditionCombination = boxTakeConversionConditionService.boxTakeConversionConditionCombination(boxPoolVO.getId());
            boxPoolVO.setConditionName(boxTakeConversionConditionCombination.getConditionName());
            boxPoolVO.setCollectionName(boxTakeConversionConditionCombination.getCollectionName());
            if (Objects.nonNull(boxPoolVO.getDaoId())) {
                GetDaoByIdVO getDaoByIdVO = userCreatorInfoDao.getDaoById(boxPoolVO.getDaoId());
                if (Objects.nonNull(getDaoByIdVO)) {
                    boxPoolVO.setDaoName(getDaoByIdVO.getDaoName());
                }
            }
        });
        return PageResult.success(boxPoolVOPage);
    }

    @PostMapping(value = "/voucherConversionStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "商品兑换上下架", notes = "商品兑换上下架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result boxPoolStatus(@Valid @RequestBody BoxPoolStatusRequest request) {
        BoxTakePool boxTakePool = boxTakePoolDao.getById(request.getId());
        if (ObjectUtil.isNull(boxTakePool)) {
            return Result.fail("未找到兑换商品");
        }
        boxTakePool.setStatus(request.getStatus());
        boxTakePool.updateById();
        return Result.success();
    }


    @PostMapping(value = "/modifiedRanking", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改排序", notes = "修改排序", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result modifiedRanking(@Valid @RequestBody BoxPoolRankingRequest request) {
        BoxTakePool boxTakePool = boxTakePoolDao.getById(request.getId());
        if (ObjectUtil.isNull(boxTakePool)) {
            return Result.fail("未找到兑换商品");
        }
        boxTakePool.setRanking(request.getRanking());
        boxTakePool.updateById();
        return Result.success();
    }

    @PostMapping(value = "/deleteById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除盲盒", notes = "删除盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result deleteById(@Valid @RequestBody IdRequest idRequest) {
        BoxTakePool boxTakePool = boxTakePoolDao.getById(idRequest.getId());
        if (boxTakePool.getStatus()) {
            throw new GlobalRunTimeException("上架中无法删除");
        }
        boxTakePool.deleteById();
        return Result.success();
    }
}
